<?php

namespace JsonLowCode\Table;

use Closure;
use JsonLowCode\Helps\Random;
use JsonLowCode\Schema;
use JsonLowCode\Support\Js;

abstract class BaseColumn
{
    protected ?Schema $element = null;
    protected ?Column $column = null;
    protected ?Closure $closure = null;
    protected array $colors = [];
    protected array $loopArgs = ["item","index"];
    protected bool $isLoop = false; //是否循环
    protected string $uuid = "";

    /**
     * 列字段变量
     * @var string
     */
    public string $columnField = "";
    /**
     * 行字段变量
     * @var string|mixed
     */
    public string $rowDataField = "";

    protected mixed $children = null;

    /**
     * @param array $parameters
     */
    public function __construct(array $parameters = [])
    {
        $this->uuid = Random::alpha(8);
        foreach ($parameters as $parameter){
            if ($parameter instanceof Closure){
                $this->closure = $parameter;
            }elseif ($parameter instanceof Column){
                $this->column = $parameter;
            }
        }
        $this->rowDataField = $this->column->rowDataField;
        $this->columnField = $this->rowDataField.".".$this->getField();
    }

    /**
     * @return Schema|null
     */
    public function element(): ?Schema
    {
        return $this->element;
    }

    /**
     * 设置循环与参数
     * @param string $itemKey item参数
     * @param string $indexKey index参数
     * @return $this
     */
    public function loop(string $itemKey = 'item', string $indexKey = 'index'): static
    {
        $this->isLoop = true;
        $this->loopArgs = [$itemKey, $indexKey];
        return $this;
    }

    protected function children(mixed $children): static
    {
        $this->children = $children;
        return $this;
    }

    /**
     * 获取字段
     * @return string
     */
    public function getField(): string
    {
       return $this->column->getField();
    }

    /**
     * 是否映射字段
     * @return bool
     */
    public function isMap(): bool
    {
        return $this->column->isMap();
    }

    /**
     * 是否映射字段
     * @return bool
     */
    public function isColor(): bool
    {
        return $this->column->isColor();
    }

    /**
     * 获取字段映射绑定
     * @return string
     */
    public function getColumnKey(): string
    {
        if($this->isMap()){
            return "{$this->columnField}_map";
        }else{
            return $this->columnField;
        }
    }

    /**
     * 获取组件颜色映射绑定
     * @return string
     */
    public function getColorBindKey(): string
    {
        if ($this->isLoop){
            $indexKey = $this->loopArgs[1];
            $color = Js::this($indexKey);
            return "{$this->columnField}_color[{$color}]";
        }else{
            return "{$this->columnField}_color";
        }
    }

    public function getLoopValueExpression(): array
    {
        $itemKey = $this->loopArgs[0];
        return JSExpression(Js::this($itemKey));
    }

    public function getNormalValueExpression(): array
    {
        return JSExpression($this->getColumnKey());
    }

    abstract protected function booted();

    protected function render(): void
    {
        $this->booted();
        !is_null($this->closure)&&call_user_func($this->closure, $this->element, $this);
        $this->element->children($this->children);
        $this->column->render($this->element);
    }

    public function __destruct()
    {
        $this->render();
    }
}
